﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using RN.ERP.Web.ModelService;
using RN.ERP.Web.BasicService;
using Newtonsoft.Json;
using RN.ERP.Web.Utilities;

namespace RN.ERP.Web.UserControl
{
    public partial class UCVerifyChange : BaseUserControl
    {
        [BindableAttribute(true)]
        public string SetID
        {
            get { return hdnSetId.Value; }
            set { hdnSetId.Value = value; }
        }
        [Browsable(true)]     //该项属性是否会显示在控件的“属性”窗口中 
        [Description("变更类型")]       //属性的说明  
        [BindableAttribute(true)]
        public ChangeTypeEnum ChangeType
        {
            get { return (ChangeTypeEnum)Enum.Parse(typeof(ChangeTypeEnum), Convert.ToString(this.hdnChangeType.Value)); }
            set { hdnChangeType.Value = value.ToString(); }
        }
        private BasicServiceClient basicService = new BasicServiceClient();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
                BindDwvHistory(SetID);
            }
        }

        private void InitPage()
        {

            if (base.UserInfo.RoleType != "Manager")
            {
                divOperation.Visible = false;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(Request.QueryString["corpUserGuid"]))
                {
                    hdnCorpUserGuid.Value = Request.QueryString["corpUserGuid"];

                }
                //MainContent_InfoChange1_hdnFunctionType
                HiddenField hdn = (HiddenField)Parent.FindControl("hdnVerifyFunType");
                if (hdn != null)
                {
                    FunctionTypeEnum funType = (FunctionTypeEnum)Enum.Parse(typeof(FunctionTypeEnum), Convert.ToString(hdn.Value));
                    ////主管查看，隐藏执行审核操作的控件
                    if (funType.Equals(FunctionTypeEnum.View))
                    {
                        divOperation.Visible = false;
                    }
                    else
                    {
                        divOperation.Visible = true;
                    }
                }
            }
        }

        /// <summary>
        /// 显示操作记录
        /// </summary>
        /// <param name="SetID"></param>
        private void BindDwvHistory(string SetID)
        {
            string result = basicService.AuditLogs(SetID, UserInfo.CurrentArea);
            if (!string.IsNullOrWhiteSpace(result) && !result.Equals("null"))
            {
                List<User_Log> logs = JsonConvert.DeserializeObject<List<User_Log>>(result);
                dwvHistory.DataSource = logs;
                dwvHistory.DataBind();
            }

        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            AuditChangeRecords();
        }

        private void AuditChangeRecords()
        {
            string mess = string.Empty;
            if (string.IsNullOrWhiteSpace(SetID) || dd_verifyStatus.SelectedValue.Equals("-1"))
            {
                return;
            }

            string oldFlowcode = Request.QueryString["flowcode"] ?? "";
            if (oldFlowcode == "审核通过" || oldFlowcode == "主管退回")
            {
                string returnUrl = string.Empty;
                if (ChangeType == ChangeTypeEnum.Corp)
                {
                    returnUrl = "location.href='../EnterpriseChange/ChangeList.aspx';";
                }
                else
                {
                    returnUrl = "location.href='../PersonChange/ChangAuditList.aspx';";
                }
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('已被" + oldFlowcode + ",无需再次审核');" + returnUrl, true);
            }
            else
            {
                ////执行变更
                string operationResult = basicService.AuditChange(SetID, dd_verifyStatus.SelectedValue, dd_vierifymemo.Value, true, UserInfo.CorpUserGuid, UserInfo.RoleId, UserInfo.UserName, UserInfo.CurrentArea);
                if (!string.IsNullOrWhiteSpace(operationResult) && !operationResult.Equals("null"))
                {
                    OperationResult opr = JsonConvert.DeserializeObject<OperationResult>(operationResult);
                    if (opr.Result)
                    {
                        string returnUrl = string.Empty;
                        if (ChangeType == ChangeTypeEnum.Corp)
                        {
                            returnUrl = "location.href='../EnterpriseChange/ChangeList.aspx';";
                        }
                        else
                        {
                            returnUrl = "location.href='../PersonChange/ChangAuditList.aspx';";
                        }
                        //string returnUrl = string.Format("location.href='../Person/ChangeRecordList.aspx?corpUserGuid={0}';", hdnCorpUserGuid.Value);
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + opr.Message + "');" + returnUrl, true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + opr.Message + "');", true);
                    }

                }
            }

        }
    }
}